<template>
  <div class="price">
    <a-form-model
      ref="priceForm"
      :model="priceForm"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-model-item label="学习包数量" v-if="classMValue === 0">
        {{ this.learning.length || 0 }}
      </a-form-model-item>
      <a-form-model-item label="学习包金额" v-if="classMValue === 0">
        {{ learnMoney.toFixed(2) }}
      </a-form-model-item>
      <a-form-model-item label="课程服务数量" v-if="classMValue === 1">
        {{ this.learning.length || 0 }}
      </a-form-model-item>
      <a-form-model-item label="课程服务金额" v-if="classMValue === 1">
        {{ service.toFixed(2) }}
      </a-form-model-item>
      <a-form-model-item label="建议对外出售原价">
        {{ (learnMoney && learnMoney.toFixed(2)) || (service && service.toFixed(2)) }}
      </a-form-model-item>
      <a-form-model-item label="对外出售价格" prop="forSale">
        <a-input v-model="priceForm.forSale" placeholder="请输入对外出售价格" />
      </a-form-model-item>
      <a-form-model-item label="最低出售价格" prop="lowest" class="tip-name">
        <a-input v-model="priceForm.lowest" placeholder="请输入最低出售价格" />
        <a-tooltip>
          <template slot="title"> 最低出售价格不可低于对外出售价格的20% </template>
          <a-icon type="info-circle" />
        </a-tooltip>
      </a-form-model-item>
      <div style="text-align: center">
        <a-button @click="prevStep"> 上一步 </a-button>
        <a-button type="primary" @click="sbumit" style="margin: 0 10px"> 提交 </a-button>
        <a-button type="primary" @click="sbumitAdd"> 提交并添加 </a-button>
      </div>
    </a-form-model>
  </div>
</template>
<script>
import { add, edit } from '@/api/product/goods'
// const validateLowest = (rule, value, callback) => {
//   if (value === '') {
//     return callback(new Error('请输入最低出售价格'))
//   } else if (this.learnMoney && value <= Math.ceil(this.learnMoney * 0.2)) {
//     return callback(new Error('不可低于套餐价的百分之20'))
//   } else {
//     return callback()
//   }
// }
export default {
  props: {
    classMValue: {
      type: Number,
      default: undefined
    },
    params: {
      type: Object,
      default: () => {
        return {
          edit: false,
          packageId: ''
        }
      }
    }
  },
  data () {
    return {
      labelCol: { span: 6 },
      wrapperCol: { span: 12 },
      priceForm: {
        forSale: JSON.parse(localStorage.getItem('info')).forSale || '',
        lowest: JSON.parse(localStorage.getItem('info')).lowest || ''
      },
      rules: {
        forSale: [{ required: true, message: '请输入对外出售价格', trigger: 'blur' }],
        // lowest: [{ required: true, validator: validateLowest, trigger: 'change' }]
        lowest: [{ required: true, message: '请输入对外出售价格', trigger: 'blur' }]
      },
      info: {},
      learning: []
    }
  },
  mounted () {
    this.info = JSON.parse(localStorage.getItem('info')) || {}
    this.learning = JSON.parse(localStorage.getItem('learning')) || []
  },
  computed: {
    learnMoney () {
      let money = 0
      this.learning.forEach(item => {
        money += item.studyPackagePrice
      })
      return money
    },
    service () {
      let money = 0
      this.learning.forEach(item => {
        money += item.number * item.servicePrice
      })
      return money
    }
  },
  methods: {
    // 上一步
    prevStep () {
      this.$emit('prevStep')
    },
    // 提交
    sbumit () {
      this.$refs.priceForm.validate(valid => {
        if (valid) {
          this.save()
        } else {
          return false
        }
      })
    },
    // 提交并添加
    sbumitAdd () {
      this.$refs.priceForm.validate(valid => {
        if (valid) {
          this.save('add')
        } else {
          return false
        }
      })
    },
    save (saveAdd) {
      const data = {
        classMode: this.info.model,
        divisionId: this.info.affiliation,
        isActive: this.info.status,
        isHighEndClass: this.info.highClass,
        isLowPriceClass: this.info.lowClass,
        lowPrice: Number(this.priceForm.lowest),
        openTime: this.info.autoDate,
        originalPrice: this.learnMoney || this.service,
        packageAttribute: this.info.attr,
        packageName: this.info.name,
        packageTypeId: this.info.kcType,
        productType: this.info.cpType,
        saleChannelType: this.info.channel.join(),
        sellPrice: Number(this.priceForm.forSale),
        sort: Number(this.info.sort),
        packageCourseServiceVOS: []
      }
      if (this.info.packageId && this.params.edit) {
        data.packageId = this.info.packageId
      }
      if (this.info.directory[0]) {
        data.directoryId = this.info.directory[0]
      }
      if (this.info.directory[1]) {
        data.examId = this.info.directory[1]
      }
      if (this.info.directory[2]) {
        data.subjectId = this.info.directory[2]
      }
      if (this.info.directory[3]) {
        data.fourExamId = this.info.directory[3]
      }
      if (this.info.directory[4]) {
        data.fiveExamId = this.info.directory[4]
      }
      if (this.info.directory[5]) {
        data.sixExamId = this.info.directory[5]
      }
      this.learning.forEach(item => {
        if (this.classMValue === 0) {
          data.packageCourseServiceVOS.push({
            studyAndCourseServiceId: item.studyPackageId
          })
        } else {
          data.packageCourseServiceVOS.push({
            serviceNum: Number(item.number)
          })
        }
      })
      if (this.params.edit) {
        edit(data).then(res => {
          if (res.code === '1') {
            this.$message.success(res.msg)
            localStorage.removeItem('info')
            localStorage.removeItem('learning')
            if (saveAdd) {
              this.$emit('sbumitAdd', 0)
              return false
            }
            this.$router.push({ name: 'goods' })
          }
        }).catch(err => {
          this.$message.error(err.msg)
        })
      } else {
        add(data).then(res => {
          if (res.code === '1') {
            this.$message.success(res.msg)
            localStorage.removeItem('info')
            localStorage.removeItem('learning')
            if (saveAdd) {
              this.$emit('sbumitAdd', 0)
              return false
            }
            this.$router.push({ name: 'goods' })
          }
        }).catch(err => {
          this.$message.error(err.msg)
        })
      }
    }
  }
}
</script>
<style lang="less" scoped>
.price {
  width: 50%;
  margin: 0 auto;
}
</style>
<style lang="less">
.price {
  .tip-name {
    .ant-col-12 {
      width: 56%;
      input {
        width: 90%;
        margin-right: 2%;
      }
    }
  }
}
</style>
